package com.xxl.job.executor.structure;

import java.util.Stack;

public class ReverseByStack {
    public static void reverseStack(Stack<Integer> st) {
        Stack<Integer> reverseStack = new Stack<>();
        while (!st.isEmpty()) {
            if (reverseStack.isEmpty()) {
                reverseStack.push(st.pop());
            }
            int mid = st.pop();
            while (!reverseStack.isEmpty() && reverseStack.peek() < mid) {
                st.push(reverseStack.pop());
            }
            reverseStack.push(mid);
        }
        while (!reverseStack.isEmpty()) {
            st.push(reverseStack.pop());
        }
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(5);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        reverseStack(stack);
        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}
